package com.one.rope.mvp.web.common.auth;

import com.one.rope.mvp.basecore.bean.ResponseMessage;
import com.one.rope.mvp.basecore.util.JsonUtils;
import com.one.rope.mvp.basecore.util.StringUtils;
import com.one.rope.mvp.basecore.bean.AdminBusinessMessage;
import com.one.rope.mvp.basecore.bean.UserBusinessMessage;
import com.one.rope.mvp.basecore.exception.BusinessDataException;
import com.one.rope.mvp.web.common.jwt.JwtUtil;
import com.one.rope.mvp.web.common.jwt.PayloadBean;
import com.one.rope.mvp.web.common.redis.AuthRedisOperator;
import com.one.rope.mvp.web.config.GlobalConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Map;

/**
 * @author Weijian.liu
 * @desc Redis Token管理
 * @date 2018/12/20
 */
@Service
@Slf4j
public class RedisTokenManager implements TokenManager {

  @Override
  public String saveAdminUserToken(AdminUserInfo userInfo) {
    /**
     * 生成token
     */
    PayloadBean payloadBean = new PayloadBean();
    payloadBean.setUserId(userInfo.getAdminUser().getId());
    payloadBean.setCreateTime(System.currentTimeMillis());
    payloadBean.setExpires(GlobalConfig.ADMIN_DEFAULT_EXPIRES_MILLISECOND);
    Map<String, Object> payloadMap = JsonUtils.objectToMapObject(payloadBean);
    String token = JwtUtil.createToken(payloadMap);
    if (StringUtils.isNullOrEmpty(token)) {
      log.error("生成登录标识错误");
      throw new BusinessDataException(AdminBusinessMessage.GENERATE_TOKEN_FAIL);
    }

    try {
      // 刷新用户缓存信息
      String oldToken = AuthRedisOperator.getAdminUserloginUserId(String.valueOf(userInfo.getAdminUser().getId()));
      if (null != oldToken) {
        // 删除旧的缓存信息
        AuthRedisOperator.deleteAdminUserLoginToken(oldToken);
      }
      // 保存用户新的token
      AuthRedisOperator.saveAdminUserLoginUserId(token, userInfo);
      // 保存用户新的缓存信息
      AuthRedisOperator.saveAdminUserLoginToken(token, userInfo);
    }catch (Exception e){
      throw new BusinessDataException(ResponseMessage.systemError(e));
    }

    return token;
  }

  @Override
  public AdminUserInfo getAdminUserLoginInfo(String token) throws Exception{
    return AuthRedisOperator.getAdminUserLoginInfo(token);
  }

  @Override
  public boolean deleteAdminUserId(String key) {
    try {
      if (AuthRedisOperator.deleteAdminUserLoginUserId(key) > 0) {
        return true;
      }
    }catch (Exception e){
      throw new BusinessDataException(ResponseMessage.systemError(e));
    }
    return false;
  }

  @Override
  public boolean deleteAdminUserToken(String key) {
    try {
      if (AuthRedisOperator.deleteAdminUserLoginToken(key) > 0) {
        return true;
      }
    }catch (Exception e){
      throw new BusinessDataException(ResponseMessage.systemError(e));
    }
    return false;
  }

  @Override
  public boolean deleteUserId(String key) {
    try {
      if (AuthRedisOperator.deleteUserLoginUserId(key) > 0) {
        return true;
      }
    }catch (Exception e){
      throw new BusinessDataException(ResponseMessage.systemError(e));
    }
    return false;
  }

  @Override
  public boolean deleteUserToken(String key) {
    try {
      if (AuthRedisOperator.deleteUserLoginToken(key) > 0) {
        return true;
      }
    }catch (Exception e){
      throw new BusinessDataException(ResponseMessage.systemError(e));
    }
    return false;
  }

  @Override
  public String saveUserToken(UserInfo userInfo) {
    /**
     * 生成token
     */
    PayloadBean payloadBean = new PayloadBean();
    payloadBean.setUserId(userInfo.getMemberInfo().getId());
    payloadBean.setCreateTime(System.currentTimeMillis());
    payloadBean.setExpires(GlobalConfig.USER_DEFAULT_EXPIRES_MILLISECOND);
    Map<String, Object> payloadMap = JsonUtils.objectToMapObject(payloadBean);
    String token = JwtUtil.createToken(payloadMap);
    if (StringUtils.isNullOrEmpty(token)) {
      log.error("生成登录标识错误");
      throw new BusinessDataException(UserBusinessMessage.GENERATE_TOKEN_FAIL);
    }

    try {
      // 刷新用户缓存信息
      String oldToken = AuthRedisOperator.getUserloginUserId(String.valueOf(userInfo.getMemberInfo().getId()));
      if (null != oldToken) {
        // 删除旧的缓存信息
        AuthRedisOperator.deleteUserLoginToken(oldToken);
      }
      userInfo.setToken(token);
      // 保存用户新的token
      AuthRedisOperator.saveUserLoginUserId(token, userInfo);
      // 保存用户新的缓存信息
      AuthRedisOperator.saveUserLoginToken(token, userInfo);
    }catch (Exception e){
      throw new BusinessDataException(ResponseMessage.systemError(e));
    }

    return token;
  }

  @Override
  public UserInfo getUserLoginInfo(String token) throws Exception{
      return AuthRedisOperator.getUserLoginInfo(token);
  }
}
